package com.ruoyi.web.controller.web3j;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web3jutil.Web3jUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.tx.Contract;

/**
 * 区块链智能合约控制器
 * 
 * @author ruoyi
 */
@Api(tags = "区块链智能合约管理")
@RestController
@RequestMapping("/web3j/contract")
public class Web3jContractController {

    /**
     * 连接智能合约
     * 
     * @param contractAddress 合约地址
     * @param privateKey 操作者私钥
     * @param contractClassName 合约类全名
     * @return 连接结果
     */
    @ApiOperation(value = "连接智能合约", notes = "通过合约地址和私钥连接到指定的智能合约")
    @PostMapping("/connect")
    public AjaxResult connectContract(
            @ApiParam(value = "合约地址", required = true) @RequestParam("contractAddress") String contractAddress,
            @ApiParam(value = "操作者私钥", required = true) @RequestParam("privateKey") String privateKey,
            @ApiParam(value = "合约类全名", required = true) @RequestParam("contractClassName") String contractClassName) {
        try {
            // 获取Web3j连接
            Web3j web3j = Web3jUtil.getWeb3j();
            
            // 创建凭证
            Credentials credentials = Credentials.create(privateKey);
            
            // 加载合约类
            Class<?> contractClass = Class.forName(contractClassName);
            if (!Contract.class.isAssignableFrom(contractClass)) {
                return AjaxResult.error("提供的类不是有效的合约类");
            }
            
            // 连接合约
            @SuppressWarnings("unchecked")
            Contract contract = Web3jUtil.loadContract(web3j, credentials, contractAddress, 
                    (Class<? extends Contract>) contractClass);
            
            return AjaxResult.success("连接合约成功", new ContractInfo(
                    contract.getContractAddress(),
                    contract.getTransactionReceipt().isPresent() ? 
                            contract.getTransactionReceipt().get().getTransactionHash() : "",
                    contractClassName
            ));
        } catch (ClassNotFoundException e) {
            return AjaxResult.error("找不到指定的合约类: " + e.getMessage());
        } catch (Exception e) {
            return AjaxResult.error("连接合约失败: " + e.getMessage());
        }
    }

    /**
     * 合约信息内部类
     */
    static class ContractInfo {
        private String contractAddress;
        private String transactionHash;
        private String contractClassName;

        public ContractInfo(String contractAddress, String transactionHash, String contractClassName) {
            this.contractAddress = contractAddress;
            this.transactionHash = transactionHash;
            this.contractClassName = contractClassName;
        }

        public String getContractAddress() {
            return contractAddress;
        }

        public void setContractAddress(String contractAddress) {
            this.contractAddress = contractAddress;
        }

        public String getTransactionHash() {
            return transactionHash;
        }

        public void setTransactionHash(String transactionHash) {
            this.transactionHash = transactionHash;
        }

        public String getContractClassName() {
            return contractClassName;
        }

        public void setContractClassName(String contractClassName) {
            this.contractClassName = contractClassName;
        }
    }
}